package com.goodving.guli.activity;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.goodving.guli.R;
import com.goodving.guli.bt.C0668a;
import com.goodving.guli.bt.DimPanelFragment;
import com.goodving.guli.bt.Utils;
import com.goodving.guli.bt.eventbus.EventUserInfoUpdate;
import com.goodving.guli.bt.eventbus.EventWeightWithUser;
import com.goodving.guli.bt.model.Birthday;
import com.goodving.guli.bt.model.C1981P;
import com.goodving.guli.bt.model.PersonInfo;
import com.goodving.guli.bt.model.WeightInfo;
import com.goodving.guli.bt.model.WeightInfoManager;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.profile.scale.ScaleWeightInfo;
import com.goodving.guli.bt.ui.C1972G;
import com.goodving.guli.bt.utils.WeightUtil;
import com.goodving.guli.bt.utils.aE;
import com.goodving.guli.bt.weight.C1974I;
import com.goodving.guli.bt.weight.UserInfo;
import com.goodving.guli.bt.weight.UserInfoManager;
import com.goodving.guli.model.DeviceSource;
import com.goodving.guli.model.Keeper;
import com.goodving.guli.view.BitmapDescriptorFactory;
import com.goodving.guli.view.C3247b;
import com.goodving.guli.view.C3657a;

import cn.com.smartdevices.bracelet.chart.LinePieChartView;
import de.greenrobot.event.EventBus;


/**
 * Created by john on 2015/12/9.
 */
public class MemberInfoSetWeightActivity extends MemberInfoBaseActivity implements View.OnClickListener, C1974I {
    public static boolean f11435h = false;
    private static final String f11436i = "MemberInfoSetWeightActivity";
    private LinePieChartView f11437j;
    private TextView f11438k;
    private TextView f11439l;
    private ImageView f11440m;
    private TextView f11441n;
    private View f11442o;
    private Context f11443p;
    private boolean f11444q;
    private boolean f11445r;
    private Handler f11446s;
    private float f11447t;
    private boolean f11448u;
    private long f11449v;

    static {
        f11435h = false;
    }

    class C2020e extends Handler {
        final /* synthetic */ MemberInfoSetWeightActivity f11471a;

        C2020e(MemberInfoSetWeightActivity memberInfoSetWeightActivity) {
            this.f11471a = memberInfoSetWeightActivity;
        }

        public void handleMessage(Message message) {
            super.handleMessage(message);
            if (message.what == 123) {
                this.f11471a.f11441n.setText(this.f11471a.f11447t + C3657a.f18328f);
                GuliLog.tracePrintInfo("MemberInfoSetWeightActivity", "mWeightText.setText " + this.f11471a.f11447t);
            }
        }
    }

    public MemberInfoSetWeightActivity() {
        this.f11442o = null;
        this.f11443p = this;
        this.f11444q = false;
        this.f11445r = false;
        this.f11446s = new C2020e(this);
        this.f11447t = 0.0f;
        this.f11448u = false;
        this.f11449v = 0;
    }

    private void m15833a(ScaleWeightInfo scaleWeightInfo) {
        GuliLog.tracePrintInfo("isManualSetWeight", this.f11444q + C3657a.f18328f);
        GuliLog.tracePrintInfo(f11436i, "weightadvdata " + scaleWeightInfo.toString());
        C1981P c1981p = new C1981P();
        c1981p.weightInfo = scaleWeightInfo;
        this.f11447t = scaleWeightInfo.m25499j();
        if (scaleWeightInfo.isStable() && scaleWeightInfo.isFinish()) {
            this.f11447t = 0.0f;
        }
        this.f11446s.sendEmptyMessage(123);
        if (scaleWeightInfo.isStable() && !scaleWeightInfo.isFinish()) {
            f11435h = false;
            C0668a.m8128a(this.f11443p, C0668a.gk, C0668a.gm);
            UserInfo userInfo = new UserInfo();
            userInfo.name = Keeper.getCurrentUserInfoByTag(Keeper.CURRENT_USER_NAME);
            String currentUserInfoByTag = Keeper.getCurrentUserInfoByTag(Keeper.CURRENT_USER_GENDER);
            if (currentUserInfoByTag.isEmpty()) {
                userInfo.gender = 1;
            } else {
                userInfo.gender = Integer.parseInt(currentUserInfoByTag);
            }
            userInfo.birthday = Keeper.getCurrentUserInfoByTag(Keeper.CURRENT_USER_BIRTH);
            currentUserInfoByTag = Keeper.getCurrentUserInfoByTag(Keeper.CURRENT_USER_HEIGHT);
            if (currentUserInfoByTag.isEmpty()) {
                userInfo.height = 100;
            } else {
                userInfo.height = Integer.parseInt(currentUserInfoByTag);
            }
            userInfo.weight = WeightUtil.m15717c(this.f11447t, c1981p.weightInfo.m25497h(), 0);
            scaleWeightInfo.m25482a(WeightUtil.m15693a(userInfo.height, Birthday.fromStr(userInfo.birthday).getAge(), userInfo.weight));
            // here i need to check if this is for person info
            PersonInfo personInfo = Keeper.readPersonInfo();
            if (personInfo.nickname.equals(Keeper.getCurrentUserInfoByTag(Keeper.CURRENT_USER_NAME)) &&
                    String.valueOf(personInfo.uid).equals(Keeper.getCurrentUserInfoByTag(Keeper.CURRENT_USER_ID))) {
                // we need to get all these information
                personInfo.weight = userInfo.weight;
                Keeper.keepPersonInfo(personInfo);
            }

            new C2021f(this, userInfo, scaleWeightInfo).execute(new Void[0]);
        }
    }

    class C2022g implements aE {
        final /* synthetic */ C2021f f11475a;

        C2022g(C2021f c2021f) {
            this.f11475a = c2021f;
        }

        public void onSuccess() {
            GuliLog.tracePrintInfo("MemberInfoSetWeightActivity", "syncinfowithserver onSucess");
            UserInfoManager.getInstance().syncWithServer(this.f11475a.f11474c.f11443p);
            WeightInfoManager.getInstance().syncFromServer(this.f11475a.f11474c.f11443p);
            this.f11475a.f11474c.f11448u = true;
            C0668a.m8126a(this.f11475a.f11474c.f11443p, C0668a.fo);
        }

        public void onFailure() {
            GuliLog.tracePrintInfo("MemberInfoSetWeightActivity", "syncinfowithserver onFailure");
            this.f11475a.f11474c.f11448u = false;
            C0668a.m8126a(this.f11475a.f11474c.f11443p, C0668a.fp);
        }
    }

    class C2021f extends AsyncTask<Void, Void, Boolean> {
        final /* synthetic */ UserInfo f11472a;
        final /* synthetic */ ScaleWeightInfo f11473b;
        final /* synthetic */ MemberInfoSetWeightActivity f11474c;

        C2021f(MemberInfoSetWeightActivity memberInfoSetWeightActivity, UserInfo userInfo, ScaleWeightInfo scaleWeightInfo) {
            this.f11474c = memberInfoSetWeightActivity;
            this.f11472a = userInfo;
            this.f11473b = scaleWeightInfo;
        }

        protected Boolean m15861a(Void... voidArr) {
            GuliLog.tracePrintInfo("MemberInfoSetWeightActivity", "doInBackground");
            GuliLog.tracePrintInfo("MemberInfoSetWeightActivity", "start sync with server " + WeightUtil.f11309k);
            // TBD hack here, to let the program to think it is success to sync with server.
            this.f11474c.f11448u = false;
            // this.f11474c.f11448u = true;
            WeightUtil.f11309k = true;
            UserInfoManager.getInstance().m15593b(this.f11474c.f11443p, new C2022g(this));
            WeightUtil.f11309k = false;
            GuliLog.tracePrintInfo("MemberInfoSetWeightActivity", "end sync with server, result ok? " + this.f11474c.f11448u);
            if (!this.f11474c.f11448u) {
                return Boolean.valueOf(false);
            }
            GuliLog.tracePrintInfo("MemberInfoSetWeightActivity", "before uid " + this.f11472a.uid);
            UserInfoManager.getInstance().addInfo(this.f11472a, true);
            GuliLog.tracePrintInfo("MemberInfoSetWeightActivity", "after uid " + this.f11472a.uid);
            return Boolean.valueOf(UserInfoManager.getInstance().syncUserInfoToServer(this.f11474c.f11443p, this.f11472a));
        }

        protected void m15862a(Boolean bool) {
            Utils.m7991a((Activity) this.f11474c.f11443p);
            //  TBD hack here, to make it as sync with server success
            //if (bool.booleanValue()) {
            if (!bool.booleanValue()) {
                Keeper.saveCurrentUser(Keeper.CURRENT_USER_NAME, C3657a.f18328f);
                Keeper.saveCurrentUser(Keeper.CURRENT_USER_GENDER, C3657a.f18328f);
                Keeper.saveCurrentUser(Keeper.CURRENT_USER_BIRTH, C3657a.f18328f);
                Keeper.saveCurrentUser(Keeper.CURRENT_USER_HEIGHT, C3657a.f18328f);
                if (this.f11472a != null) {
                    WeightInfo weightInfo = new WeightInfo();
                    weightInfo.uid = this.f11472a.uid;
                    if (0 == this.f11474c.f11449v) {
                        weightInfo.timestamp = this.f11473b.m25494e();
                    } else {
                        weightInfo.timestamp = this.f11474c.f11449v;
                    }
                    weightInfo.weight = this.f11472a.weight;
                    WeightInfoManager.getInstance().m15749a(weightInfo);
                    EventBus.getDefault().post(new EventWeightWithUser(this.f11473b, this.f11472a.uid));
                    EventBus.getDefault().post(new EventUserInfoUpdate(this.f11472a));
                    this.f11474c.setResult(-1);
                    this.f11474c.finish();
                    return;
                }
                return;
            }
            GuliLog.tracePrintInfo("MemberInfoSetWeightActivity", "onSyncFail");
            UserInfoManager.getInstance().deleteInfoLocal(this.f11472a);
            C3247b.m22930a(this.f11474c.f11443p, this.f11474c.getString(R.string.please_connect_internet), 0).show();
        }

        protected /* synthetic */ Boolean doInBackground(Void[] objArr) {
            return m15861a((Void[]) objArr);
        }

        protected /* synthetic */ void onPostExecute(Boolean obj) {
            m15862a((Boolean) obj);
        }

        protected void onPreExecute() {
            super.onPreExecute();
            Utils.m7992a((Activity) this.f11474c.f11443p, (int) R.string.add_family_member);
        }
    }

    
    private void m15839e() {
        this.f11438k = (TextView) findViewById(R.id.search_devices_title);
        this.f11439l = (TextView) findViewById(R.id.search_devices_info);
        this.f11438k.setText(R.string.find_weight_scale_info);
        this.f11439l.setText(C3657a.f18328f);
        this.f11440m = (ImageView) findViewById(R.id.search_devices_mili_icon);
        this.f11440m.setImageResource(R.drawable.mi_logo);
        this.f11437j = (LinePieChartView) findViewById(R.id.searching_pie_chart);
        LayoutParams layoutParams = (LayoutParams) this.f11437j.getLayoutParams();
        layoutParams.width = (int) getResources().getDimension(R.dimen.draw_weight_icon_w);
        layoutParams.height = (int) getResources().getDimension(R.dimen.draw_weight_icon_w);
        this.f11437j.setMode(4);
        this.f11437j.d_();
        findViewById(R.id.search_single_frame).setBackgroundColor(getResources().getColor(R.color.bg_mode_weight));
        this.f11441n = (TextView) findViewById(R.id.search_devices_weight_value);
        this.f11442o = findViewById(R.id.right_button);
        this.f11442o.setOnClickListener(this);
        if (!DeviceSource.hasBindWeight()) {
            this.f11442o.performClick();
        }
    }

    public void m15563a () { // m15840a
        GuliLog.tracePrintInfo(f11436i, "onWeightSelectCancel");
        this.f11444q = false;
    }

    public void m15564a (float f) { // m15841a
        GuliLog.tracePrintInfo(f11436i, "onWeightSelect " + f);
        ScaleWeightInfo scaleWeightInfo = new ScaleWeightInfo();
        scaleWeightInfo.m25488b(WeightUtil.m15706b(f, Keeper.readPersonInfo().miliConfig.weightUnit));
        scaleWeightInfo.setUnitType(Keeper.readPersonInfo().miliConfig.weightUnit);
        scaleWeightInfo.setStable(true);
        scaleWeightInfo.setFinish(false);
        if (0 == this.f11449v) {
            this.f11449v = System.currentTimeMillis();
        }
        scaleWeightInfo.setDate(this.f11449v);
        scaleWeightInfo.m25490c(f);
        String currentUserInfoByTag = Keeper.getCurrentUserInfoByTag(Keeper.CURRENT_USER_BIRTH);
        String currentUserInfoByTag2 = Keeper.getCurrentUserInfoByTag(Keeper.CURRENT_USER_HEIGHT);
        int i = 100;
        if (!currentUserInfoByTag2.isEmpty()) {
            i = Integer.parseInt(currentUserInfoByTag2);
        }
        scaleWeightInfo.m25482a(WeightUtil.m15693a(i, Birthday.fromStr(currentUserInfoByTag).getAge(), f));
        m15833a(scaleWeightInfo);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.search_devices_exit:
                setResult(-1);
                finish();
            case R.id.left_button:
                GuliLog.tracePrintInfo(f11436i, "click left btn");
            case R.id.right_button:
                C0668a.m8128a(this.f11443p, C0668a.gk, C0668a.gl);
                DimPanelFragment.showPanel((Activity) this.f11443p, C1972G.m15555a(this.f11445r ? 8.0f : BitmapDescriptorFactory.HUE_YELLOW).getClass());
                this.f11444q = true;
            default:
        }
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView((int) R.layout.member_set_weight_layout);
        this.f11445r = getIntent().getBooleanExtra("FROM_BABY_WEIGHT", false);
        m15839e();
        EventBus.getDefault().register(this);
    }

    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void onEventBackgroundThread(ScaleWeightInfo scaleWeightInfo) {
        GuliLog.tracePrintInfo(f11436i, "onEvent WeightAdvData:" + scaleWeightInfo);
        if (f11435h && !this.f11444q && scaleWeightInfo != null) {
            m15833a(scaleWeightInfo);
        }
    }

    protected void onPause() {
        super.onPause();
        f11435h = false;
    }

    protected void onResume() {
        super.onResume();
        f11435h = true;
        this.f11449v = 0;
    }
}
